package com.ks.pc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.dto.BrandDto;
import com.ks.dto.ClassifyDto;
import com.ks.dto.ImgDto;
import com.ks.dto.ProductDto;
import com.ks.pc.dao.DaoSupport;
import com.ks.pc.service.SysProductService;
import com.ks.pc.service.SysUeditorService;


/**
 * 
 * @author htpeng
 *2016��11��24������4:30:15
 */
@Service("sysUeditorService")
public class SysUeditorServiceImpl implements SysUeditorService{
	@Autowired
	private DaoSupport daoSupport;

	@Autowired
	private SysProductService sysProductService;
	
	/* (non-Javadoc)
	 * @see com.ks.pc.service.SysUeditorService#queryAllClassify()
	 */
	@Override
	public List<ClassifyDto> queryAllClassify() {
		try {
			return daoSupport.findForList("OtherMapper.queryAllClassify",null);
		} catch (Exception e) {
		}
		return null;
	}
	@Override
	public List<BrandDto> queryAllBrand() {
		try {
			return daoSupport.findForList("OtherMapper.queryAllBrand",null);
		} catch (Exception e) {
		}
		return null;
	}
	/* (non-Javadoc)
	 * @see com.ks.pc.service.SysUeditorService#queryBrandById(java.lang.String)
	 */
	@Override
	public BrandDto queryBrandById(Integer brandId) {
		try {
			return daoSupport.findForObject("OtherMapper.queryBrandById",brandId);
		} catch (Exception e) {
		}
		return null;
	}
	
	
	
	public Integer saveProduct(ProductDto productDto) {
		 try {
			Integer id = sysProductService.getMaxProductId();
			id = id == null ? 1 : id+1;
			productDto.setId(id);
			daoSupport.save("OtherMapper.saveProduct", productDto);
//			if(null!=id){
				List<String> imgIds =productDto.getImgs();
				Map<String, Object> map = new HashMap<String,Object>();
				map.put("pcId", id);
				map.put("imgIds", imgIds);
				daoSupport.save("OtherMapper.saveProductImgRel", map);
				return id;
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		 return null;
	}
	public boolean updateProductById(ProductDto dto) {
		try {
			daoSupport.delete("OtherMapper.deleteProductImgRel", dto.getId()) ;
			daoSupport.update("OtherMapper.updateProductById", dto);
			List<String> imgIds =dto.getImgs();
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("pcId", dto.getId());
			map.put("imgIds", imgIds);
			daoSupport.save("OtherMapper.saveProductImgRel", map);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/* (non-Javadoc)
	 * @see com.ks.pc.service.SysUeditorService#queryProductById(java.lang.Integer)
	 */
	@Override
	public ProductDto queryProductById(Integer productId) {
		try {
			ProductDto p= daoSupport.findForObject("OtherMapper.queryProductById",productId);
			List<ImgDto> list=daoSupport.findForList("OtherMapper.queryProductImgs",productId);
			if(null==list){
				list=new ArrayList<ImgDto>();
			}
			p.setImgDtos(list);
			return p;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
